﻿Imports DTO
Imports System.Data.SqlClient
Public Class CQuyenDAO

    Public Function LoadQuyen() As List(Of CQuyenDTO)
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("uspLoadQuyen", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim lquyen As New List(Of CQuyenDTO)
            Using reader As IDataReader = cm.ExecuteReader(CommandBehavior.CloseConnection)
                While reader.Read()
                    lquyen.Add(New CQuyenDTO(reader))
                End While
            End Using
            Return lquyen
        End Using
    End Function

    Public Function LayQuyen(ByVal maquyenhan As Integer) As CQuyenDTO
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("uspLayQuyen", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim para As SqlParameter = New SqlParameter("@maquyenhan", maquyenhan)
            cm.Parameters.Add(para)
            Using reader As IDataReader = cm.ExecuteReader(CommandBehavior.CloseConnection)
                If reader.Read() Then
                    Return New CQuyenDTO(reader)
                Else
                    Return Nothing
                End If
            End Using
        End Using
    End Function

    Public Function UpdateQuyen(ByVal quyen As CQuyenDTO) As Integer
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("uspCapNhatQuyen", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim para As SqlParameter = New SqlParameter("@ma", quyen.Ma)
            cm.Parameters.Add(para)

            para = New SqlParameter("@dulieu", quyen.RibbonBarDuLieu)
            cm.Parameters.Add(para)

            para = New SqlParameter("@muontrasach", quyen.RibbonBarMuonTraSach)
            cm.Parameters.Add(para)
            para = New SqlParameter("@nhanvien", quyen.RibbonBarNhanVien)
            cm.Parameters.Add(para)
            para = New SqlParameter("@quanlysach", quyen.RibbonBarQuanLySach)
            cm.Parameters.Add(para)
            para = New SqlParameter("@thethuvien", quyen.RibbonBarTheThuVien)
            cm.Parameters.Add(para)
            para = New SqlParameter("@thutienphat", quyen.RibbonBarThuTienPhat)
            cm.Parameters.Add(para)
            para = New SqlParameter("@timkiemtheodocgia", quyen.RibbonBarTimKiemTheoDocGia)
            cm.Parameters.Add(para)
            para = New SqlParameter("@timkiemtheosach", quyen.RibbonBarTimKiemTheoSach)
            cm.Parameters.Add(para)
            para = New SqlParameter("@tinhhinhchomuonsach", quyen.RibbonBarTinhHinhChoMuonSach)
            cm.Parameters.Add(para)
            para = New SqlParameter("@admin", quyen.RibbonTabIemAdmin)
            cm.Parameters.Add(para)
            para = New SqlParameter("@blackadmin", quyen.RibbonTabItemBlackAdmin)
            cm.Parameters.Add(para)

            cm.ExecuteNonQuery()
            cn.Close()
            Return 1
        End Using
    End Function
End Class
